/**************************************************************
 *@ProjectName: blweibo
 *@FileName: Comment.java
 *@Date: 2011-5-30
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo.models;

import java.io.Serializable;

import org.json.JSONObject;

import android.text.TextUtils;

import com.zhiweibo.util.TopicUtil;

/**
 * The Class Comment.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-30
 */
public class Comment implements Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 7870004747793890151L;

    /** The cid. */
    private String cid = "";

    /** The text. */
    private String text = "";

    /** The dateline. */
    private String dateline = "";

    /** The user. */
    private User user;

    /** The topic. */
    private Topic topic;

    /** The recomment. */
    private Comment recomment;

    /**
     * Instantiates a new comment.
     */
    public Comment() {
    }

    /**
     * Instantiates a new comment.
     * 
     * @param js
     *            the js
     */
    public Comment(JSONObject js) {
        if (js == null) {
            return;
        }
        cid = js.optString("cid");
        text = js.optString("text");
        dateline = js.optString("dateline");
        user = new User(js.optJSONObject("user"));
        topic = new Topic(js.optJSONObject("topic"));
        recomment = new Comment();
        if (js.optJSONObject("recomment") != null) {
            JSONObject jsob = js.optJSONObject("recomment");
            recomment.cid = jsob.optString("cid");
            recomment.text = jsob.optString("text");
            recomment.dateline = jsob.optString("dateline");
            recomment.user = new User(jsob.optJSONObject("user"));
            recomment.topic = new Topic(jsob.optJSONObject("topic"));
            recomment.recomment = new Comment();
        }
    }

    /**
     * Gets the cid.
     * 
     * @return the cid
     */
    public String getCid() {
        return cid;
    }

    /**
     * Sets the cid.
     * 
     * @param cid
     *            the new cid
     */
    public void setCid(String cid) {
        this.cid = cid;
    }

    /**
     * Gets the text.
     * 
     * @return the text
     */
    public String getText() {
        return text;
    }

    /**
     * Sets the text.
     * 
     * @param text
     *            the new text
     */
    public void setText(String text) {
        this.text = text;
    }

    /**
     * Gets the dateline.
     * 
     * @return the dateline
     */
    public String getDateline() {
        return TopicUtil.formatDate(dateline);
    }

    /**
     * Sets the dateline.
     * 
     * @param dateline
     *            the new dateline
     */
    public void setDateline(String dateline) {
        this.dateline = dateline;
    }

    /**
     * Gets the user.
     * 
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * Sets the user.
     * 
     * @param user
     *            the new user
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * Gets the topic.
     * 
     * @return the topic
     */
    public Topic getTopic() {
        return topic;
    }

    /**
     * Sets the topic.
     * 
     * @param topic
     *            the new topic
     */
    public void setTopic(Topic topic) {
        this.topic = topic;
    }

    /**
     * Gets the recomment.
     * 
     * @return the recomment
     */
    public Comment getRecomment() {
        return recomment;
    }

    /**
     * Sets the recomment.
     * 
     * @param recomment
     *            the new recomment
     */
    public void setRecomment(Comment recomment) {
        this.recomment = recomment;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Comment [cid=" + cid + ", text=" + text + ", dateline=" + dateline + ", user=" + user + ", topic="
                + topic + ", recomment=" + recomment + "]";
    }

    /**
     * To json string.
     * 
     * @return the string
     */
    public String toJSONString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"cid\"").append(":\"").append(cid).append("\",");
        sb.append("\"text\"").append(":\"").append(text).append("\",");
        sb.append("\"dateline\"").append(":\"").append(dateline).append("\",");
        sb.append("\"user\"").append(":").append(user.toJSONString());
        if (!TextUtils.isEmpty(topic.getTid())) {
            sb.append(",").append("\"topic\"").append(":").append(topic.toJSONString());
        }
        if (!TextUtils.isEmpty(recomment.getCid())) {
            sb.append(",").append("\"recomment\"").append(":").append(recomment.toJSONString());
        }
        sb.append("}");
        return sb.toString();
    }

}
